package com.example.personalizededucationsystem.repository;

import com.example.personalizededucationsystem.model.Assignment;
import com.example.personalizededucationsystem.model.StudentAssignment;
import com.example.personalizededucationsystem.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface StudentAssignmentRepository extends JpaRepository<StudentAssignment, Long> {

    Optional<StudentAssignment> findByStudentAndAssignment(User student, Assignment assignment);

    @Query("SELECT sa FROM StudentAssignment sa JOIN FETCH sa.student WHERE sa.assignment.assignmentId = :assignmentId")
    List<StudentAssignment> findByAssignmentIdWithStudent(@Param("assignmentId") Long assignmentId);

    @Query("SELECT sa FROM StudentAssignment sa WHERE sa.assignment.myClass.classId = :classId AND sa.status = 'GRADED'")
    List<StudentAssignment> findAllGradedByClassId(@Param("classId") Long classId);

    @Query("SELECT sa FROM StudentAssignment sa JOIN FETCH sa.assignment a JOIN FETCH a.myClass WHERE sa.student = :student ORDER BY a.deadline ASC")
    List<StudentAssignment> findByStudentWithDetails(@Param("student") User student);
}